package com.eelpo.cmmp.basic.pages.system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.eelpo.cmmp.basic.dao.vo.SystemConfig;
import com.eelpo.cmmp.basic.services.ISystemConfigManageService;
import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.behavior.JavascriptEventConfirmation;
import com.eelpo.cmmp.core.wicket.page.extensions.SerachListWebPage;
import com.eelpo.cmmp.core.wicket.panel.grid.column.ColumnLocation;
import com.eelpo.cmmp.core.wicket.panel.grid.column.ColumnLocation.Unit;
import com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader;
import com.eelpo.cmmp.core.wicket.panel.grid.column.OperateColumn;
import com.eelpo.cmmp.core.wicket.panel.grid.column.PropertyColumn;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.Loop.LoopItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * 
 * <pre>
 * 
 * 系统配置管理页面
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class SystemConfigManage extends SerachListWebPage<SystemConfig> {

	@SpringBean
	private ISystemConfigManageService systemConfigManageService;

	public SystemConfigManage() {
		super("系统配置管理", 15, new SystemConfig());
	}

	@Override
	public Panel renderSearchPanel(String id) {
		return new SystemConfigSearchPanel(id, getParameter());
	}

	@Override
	public Iterator<? extends SystemConfig> iterator(SystemConfig parameter, int first, int count) throws Exception {
		return systemConfigManageService.getSystemConfigsPaing(parameter, first, count).iterator();
	}

	@Override
	public int totalRows(SystemConfig parameter) throws Exception {
		return systemConfigManageService.getSystemConfigsCount(parameter);
	}

	@Override
	public List<IColumnHeader<SystemConfig>> renderColumns() {
		List<IColumnHeader<SystemConfig>> columns = new ArrayList<IColumnHeader<SystemConfig>>();
		columns.add(new PropertyColumn<SystemConfig>("系统配置键", "configKey", true, new ColumnLocation(15, Unit.PERCENT)));
		columns.add(new PropertyColumn<SystemConfig>("系统配置值", "configValue", true, new ColumnLocation(26, Unit.PERCENT)));
		columns.add(new PropertyColumn<SystemConfig>("系统配置描述信息", "description", true, new ColumnLocation(26, Unit.PERCENT)));
		columns.add(new PropertyColumn<SystemConfig>("系统配置提示信息", "hint", true, new ColumnLocation(26, Unit.PERCENT)));
		columns.add(new OperateColumn<SystemConfig>(2, new ColumnLocation(7, Unit.PERCENT)) {

			private static final long serialVersionUID = -2091936045710639843L;

			@Override
			public void populateItem(String id, String labelId, LoopItem item, IModel<SystemConfig> model) {
				switch (item.getIteration()) {
				case 0:
					item.add(renderEditLink(id, labelId, model));
					break;
				case 1:
					item.add(renderDeleteLink(id, labelId, model));
					break;
				}
			}
		});
		return columns;
	}

	public MarkupContainer renderEditLink(String id, String labelId, final IModel<SystemConfig> model) {
		Link<SystemConfig> editLink = new Link<SystemConfig>(id) {

			private static final long serialVersionUID = -5260736556615893110L;

			@Override
			public void onClick() {
				setResponsePage(new AddOrEditSystemConfig(model.getObject()));
			}
		};

		return editLink.add(new Label(labelId, "编辑"));
	}

	public MarkupContainer renderDeleteLink(String id, String labelId, final IModel<SystemConfig> model) {
		Link<SystemConfig> deleteLink = new Link<SystemConfig>(id) {

			private static final long serialVersionUID = -5260736556615893110L;

			@Override
			public void onClick() {
				try {
					systemConfigManageService.deleteSystemConfig(model.getObject());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("删除系统配置失败！");
				}
			}
		};

		// deleteLink.setEnabled(CMMPSession.get().checkPermission("", ""));

		deleteLink.add(new JavascriptEventConfirmation("您确定要删除该系统配置吗？", deleteLink.isEnabled()));

		return deleteLink.add(new Label(labelId, "删除"));
	}
}
